package wechat.rcv.cosuming;

import java.util.Date;

import org.springframework.stereotype.Component;

import wechat.rcv.RcvToken;
import wechat.rcv.util.RequestParser;

@Component
public class TxtMsgConsuming implements IMsgConsuming {
	private final String tag_content = "Content";
	private String msg;
	
	public TxtMsgConsuming() {
	}
	public TxtMsgConsuming(String xmlMsg) {
		msg = xmlMsg;
	}

	@Override
	public String acceptSync() {
		// TODO Auto-wechat.rcv.generated method stub
		return createMsg(RequestParser.getElementByTag(msg, "FromUserName"), RequestParser.getElementByTag(msg, tag_content) + " has been received");
	}

	private String createMsg(String ToUserName, String content) {
		String format =
				"<xml>\n"
				+ "<ToUserName><![CDATA[%1$s]]></ToUserName>\n"
				+ "<FromUserName><![CDATA[%2$s]]></FromUserName>\n" 
				+ "<CreateTime>%3$s</CreateTime>\n"
				+ "<MsgType><![CDATA[%4$s]]></MsgType>\n"
				+ "<Content><![CDATA[%5$s]]></Content>\n"
				+ "</xml>";
		
		String createTime = Long.toString(new Date().getTime());
		return String.format(format, ToUserName, RcvToken.WECHAT_CORP_ID, createTime, "text", content);
	}

	@Override
	public String acceptAsync() {
		// TODO Auto-wechat.rcv.generated method stub
		return null;
	}

	private RequestObj req;
	
	@Override
	public void setRequestObj(RequestObj ro) {
		// TODO Auto-generated method stub
		req = ro;
	}	
}
